﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.SOL.GLReading.Layers.Model;

namespace Wicresoft.SOL.GLReading.Controllers
{ 
    public class TeacherController : Controller
    {
        private GLReadingEntities db = new GLReadingEntities();

        //
        // GET: /Teacher/

        public ViewResult Index()
        {
            //var result = db.Student.Include("ReadingResult").ToList();
            //var result = from o in ctx.Orders.Include("OrderDetails")
                         //select o;
            //var result = from s in db.Student.Include("ReadingResult")
            //             select s;
            //var result = from s in db.Student
            //             join r in db.ReadingResult
            //             on s.ID equals r.StudentID
            //             select new
            //             {
            //                 studentNo = s.StudentNo,
            //                 name = s.Name,
            //                 score = r.TestScore
            //             };

            List<Student> students = db.Student.ToList();
            List<NewStudent> newStudentList = new List<NewStudent>();
            foreach (var item in students)
            {
                NewStudent newStudent = new NewStudent();
                newStudent.student = item;
                List<ReadingResult> a = db.ReadingResult.Where(rr => rr.StudentID == item.ID).ToList();
                if (a.Count() == 0)
                {
                    newStudent.maxScore = 0;
                    newStudent.displayScore = "0/100";
                }
                else
                {
                    //存在记录选出最大值
                    newStudent.maxScore = (int)a.Single(re => re.IsMax == true).TestScore;
                    newStudent.displayScore = string.Format(@"{0}/100", newStudent.maxScore);
                }
                newStudentList.Add(newStudent);
            }

            return View(newStudentList);
        }

        //
        // GET: /Teacher/Details/5

        public ViewResult Details(Guid id)
        {
            Student student = db.Student.Single(s => s.ID == id);
            return View(student);
        }

        //
        // GET: /Teacher/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Teacher/Create

        [HttpPost]
        public ActionResult Create(Student student)
        {
            if (ModelState.IsValid)
            {
                student.ID = Guid.NewGuid();
                db.Student.AddObject(student);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(student);
        }
        
        //
        // GET: /Teacher/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            Student student = db.Student.Single(s => s.ID == id);
            return View(student);
        }

        //
        // POST: /Teacher/Edit/5

        [HttpPost]
        public ActionResult Edit(Student student)
        {
            if (ModelState.IsValid)
            {
                db.Student.Attach(student);
                db.ObjectStateManager.ChangeObjectState(student, EntityState.Modified);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(student);
        }

        //
        // GET: /Teacher/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            Student student = db.Student.Single(s => s.ID == id);
            return View(student);
        }

        //
        // POST: /Teacher/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            Student student = db.Student.Single(s => s.ID == id);
            db.Student.DeleteObject(student);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}